package com.example.mobile2021606134chhnewdemo.NewsModule;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.mobile2021606134chhnewdemo.ContactModule.ContactActivity;
import com.example.mobile2021606134chhnewdemo.MapModule.MapActivity;
import com.example.mobile2021606134chhnewdemo.R;
import com.example.mobile2021606134chhnewdemo.VideoModule.VideoActivity;
import com.example.mobile2021606134chhnewdemo.ViewPageAdapter;
import com.example.mobile2021606134chhnewdemo.WeatherModule.WeatherActivity;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import me.sugarkawhi.bottomnavigationbar.BottomNavigationBar;
import me.sugarkawhi.bottomnavigationbar.BottomNavigationEntity;

public class NewsActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    List<Fragment> fragmentList = new ArrayList<>();
    List<BottomNavigationEntity> entities = new ArrayList<>();
    private BottomNavigationBar bottomNavigationBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_news);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
        initView();
        initTitleBar();
        initBottomBar();
    }

    private void initTitleBar() {
        View viewSearch = findViewById(R.id.viewSearch);
        ImageView imgShare = findViewById(R.id.imgShare1);
        ImageView imgMy = findViewById(R.id.imgMy);
        viewSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NewsActivity.this, SearchNewsActivity.class);
                startActivity(intent);
            }
        });
        imgShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        imgMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager2 = (ViewPager2) findViewById(R.id.viewPager2);

        fragmentList.add(HeadlineFragment.newInstance("头条", ""));
        fragmentList.add(NewsFragment.newInstance("新闻", ""));
        fragmentList.add(EconomicsFragment.newInstance("财经", ""));
        fragmentList.add(SportsFragment.newInstance("体育", ""));
        fragmentList.add(EntertainmentFragment.newInstance("娱乐", ""));
        fragmentList.add(MilitaryFragment.newInstance("军事", ""));
        fragmentList.add(EducationFragment.newInstance("教育", ""));
        fragmentList.add(ScienceFragment.newInstance("科技", ""));
        fragmentList.add(NBAFragment.newInstance("NBA", ""));
        fragmentList.add(StockFragment.newInstance("股票", ""));
        fragmentList.add(ConstellationFragment.newInstance("星座", ""));
        fragmentList.add(WomenFragment.newInstance("女性", ""));
        fragmentList.add(HealthFragment.newInstance("健康", ""));
        fragmentList.add(ParentingFragment.newInstance("育儿", ""));
        ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList);
        viewPager2.setAdapter(adapter);

        new TabLayoutMediator(tabLayout, viewPager2, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(fragmentList.get(position).getArguments().getString("title"));
            }
        }).attach();

        entities.add(new BottomNavigationEntity("通讯", R.drawable.communication_d, R.drawable.communication_s));
        entities.add(new BottomNavigationEntity("视频",  R.drawable.play_d, R.drawable.play_s));
        entities.add(new BottomNavigationEntity("   ", R.drawable.new_d, R.drawable.new_s));
        entities.add(new BottomNavigationEntity("导航", R.drawable.navigation_d, R.drawable.navigation_s));
        entities.add(new BottomNavigationEntity("天气", R.drawable.weather_d, R.drawable.weather_s));
    }

    private void initBottomBar() {
        bottomNavigationBar = findViewById(R.id.BNBNews);
        bottomNavigationBar.setEntities(entities);
        bottomNavigationBar.setCurrentPosition(2);
        bottomNavigationBar.setBnbItemDoubleClickListener(new BottomNavigationBar.IBnbItemDoubleClickListener() {
            @Override
            public void onBnbItemDoubleClick(int position) {
                Log.i("bottomNavigationBar", "重复选中"+position);
                Intent intent0 = new Intent(getApplicationContext(), NewsActivity.class);
                startActivity(intent0);
                finish();
                overridePendingTransition(0, 0);
            }
        });
        bottomNavigationBar.setBnbItemSelectListener(new BottomNavigationBar.IBnbItemSelectListener() {
            @Override
            public void onBnbItemSelect(int position) {
                Log.i("bottomNavigationBar", "选中"+position);
                switch (position) {
                    case 0:
                        Intent intent3 = new Intent(getApplicationContext(), ContactActivity.class);
                        startActivity(intent3);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 1:
                        Intent intent4 = new Intent(getApplicationContext(), VideoActivity.class);
                        startActivity(intent4);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 2:
                        break;
                    case 3:
                        Intent intent1 = new Intent(getApplicationContext(), MapActivity.class);
                        startActivity(intent1);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 4:
                        Intent intent = new Intent(getApplicationContext(), WeatherActivity.class);
                        startActivity(intent);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                }
            }
        });
    }
}